package com.generation.tool.exception;

import com.generation.tool.pojo.result.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public <T> R<T> exceptionHandler(Exception e) {
        // 先判断拦截的Exception 是否我们自己定义的异常类型
        if(e instanceof AppException) {
            AppException appException = (AppException) e;
            return R.error(appException.getCode(), appException.getMsg());
        }

        // 拦截的是未知异常
        return R.error(400, "服务器捕获未知异常");
    }
}
